package com.wise.site.model;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKBusLineResult;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKPlanNode;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKSuggestionResult;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.wise.site.R;
import com.wise.site.WiseSiteApplication;
import com.wise.site.utils.Constanct;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-8-8
 * @see : 线路搜索路径展示以及地图线路展示 
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class SearchLineActivity extends com.baidu.mapapi.MapActivity
{
    protected static final String TAG = "SearchLineActivity";
    private SearchLine mSearchLineIntentModel;
    private MKPlanNode mStartNode = new MKPlanNode();
    private MKPlanNode mEndNode = new MKPlanNode();
    private MKSearch mMKSearch = new MKSearch();
    private BMapManager mBMapMan;
    private ArrayList<String> mList;
    private String mDistance;
    private ListView mListView;
    private SearchLineAdapter mAdapter;
    
    @Override
    protected void onCreate(Bundle arg0)
    {
        if (!handIntent())
        {
            finish();
            return;
        }
        super.onCreate(arg0);
        setContentView( R.layout.activity_search_line);
        
        ((TextView)findViewById(R.id.activity_title)).setText(R.string.bus_line_title);
        
        mList = new ArrayList<String>();
        mListView = (ListView) findViewById(R.id.loadView);
        mAdapter = new SearchLineAdapter(this, mList);
        
        LayoutInflater inflater = getLayoutInflater();
        View header = inflater.inflate(R.layout.list_item_search_line, null);
        header.findViewById(R.id.search_line_img).setBackgroundResource(R.drawable.src_mark);
        ((TextView)header.findViewById(R.id.search_line_tv)).setText("您的当前位置");
        
        View foot = inflater.inflate(R.layout.list_item_search_line, null);
        foot.findViewById(R.id.search_line_img).setBackgroundResource(R.drawable.dest_mark);
        ((TextView)foot.findViewById(R.id.search_line_tv)).setText("目的地位置");
        
        mListView.addHeaderView(header);
        mListView.addFooterView(foot);
        
        mListView.setAdapter(mAdapter);
        
        mBMapMan = WiseSiteApplication.getContext().getBMapMan();
        mMKSearch.init(mBMapMan, mMKSearchListener);
        mMKSearch.transitSearch(this.mSearchLineIntentModel.startCity,
                this.mStartNode, this.mEndNode);
        
        findViewById(R.id.title_back).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
//        initViews();
//        search(mCurStatus);
    }
    
    
    private boolean handIntent()
    {
        Intent it = getIntent();
        mSearchLineIntentModel = (SearchLine) it.getSerializableExtra(Constanct.TAG_SEARCH_LINE);
        if (mSearchLineIntentModel == null)
        {
            Toast.makeText(this, "缺少起点或终点", Toast.LENGTH_LONG).show();
            return false;
        }
        mStartNode.name = this.mSearchLineIntentModel.startArea;
        mStartNode.pt = new GeoPoint(this.mSearchLineIntentModel.startY,this.mSearchLineIntentModel.startX);
        mEndNode.name = this.mSearchLineIntentModel.endArea;
//        mEndNode.pt = new GeoPoint(this.mSearchLineIntentModel.endY,this.mSearchLineIntentModel.endX);
        mEndNode.pt = new GeoPoint(40057031, 116307852);
        mMKSearch.setTransitPolicy(MKSearch.EBUS_TRANSFER_FIRST);
        
        return true;
    }
    
    MKSearchListener mMKSearchListener = new MKSearchListener()
    {

		@Override
		public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onGetBusDetailResult(MKBusLineResult result, int arg1) {
			
		}

		@Override
		public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onGetTransitRouteResult(MKTransitRouteResult paramMKTransitRouteResult, int arg1) {
			if(paramMKTransitRouteResult == null){
				finish();
				return;
			}
			
			mList.clear();
			if ((paramMKTransitRouteResult.getNumPlan() > 0)
                    && (paramMKTransitRouteResult.getPlan(0).getNumLines() > 0))
            {
                int i = paramMKTransitRouteResult.getPlan(0).getNumLines();
                int j = 0;
                for (int k = 0; k < i; k++)
                {
                    StringBuffer localStringBuffer = new StringBuffer();
                    j += paramMKTransitRouteResult.getPlan(0).getLine(k)
                            .getDistance();
                    localStringBuffer.append(paramMKTransitRouteResult
                            .getPlan(0).getLine(k).getGetOnStop().name);
                    localStringBuffer.append(paramMKTransitRouteResult
                            .getPlan(0).getLine(k).getTitle());
                    localStringBuffer.append(paramMKTransitRouteResult
                            .getPlan(0).getLine(k).getGetOffStop().name);
                    mList.add(localStringBuffer.toString());
                }
                mDistance = getDistance(j);
                mAdapter.notifyDataSetChanged();
            }
		}

		@Override
		public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
			// TODO Auto-generated method stub
			
		}
    	
    };
    
    public String getDistance(int paramInt)
    {
        return paramInt + "米";
    }
    
	@Override
	protected void onPause() {
	    if (mBMapMan != null) {
	        mBMapMan.stop();
	    }
	    super.onPause();
	}
	@Override
	protected void onResume() {
	    if (mBMapMan != null) {
	        mBMapMan.start();
	    }
	    super.onResume();
	}

    class SearchLineAdapter extends ArrayAdapter<String>
    {
    	private LayoutInflater inflater;
        public SearchLineAdapter(Context context, List<String> list)
        {
            super(context, 0, list);
            inflater = LayoutInflater.from(context);
        }

        public View getView(int paramInt, View paramView,
                ViewGroup paramViewGroup)
        {
            if (paramView == null)
            {
                paramView = inflater.inflate(R.layout.list_item_search_line,
                        null);
            }
            TextView localTextView1 = (TextView) paramView
                    .findViewById(R.id.search_line_img);
            TextView localTextView2 = (TextView) paramView
                    .findViewById(R.id.search_line_tv);
            localTextView1.setText((paramInt + 1) + "");
            localTextView2.setText((CharSequence) getItem(paramInt));
            return paramView;
        }
    }
    
    public static boolean startActivity(Context context, SearchLine param)
    {
        if(param == null || !param.isValid()) return false;
        Intent it = new Intent(context, SearchLineActivity.class);
        it.putExtra(Constanct.TAG_SEARCH_LINE, param);
        context.startActivity(it);
        return true;
    }


	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
}
